<?

/* ------------------------------------------------------------------------------------------------------------------------------ */

class ccdsFormFieldHandler
{
    private $registeredFields=array();

    private $classes=array();

    private $add_value_processing=array();

    /* ----------------------------------------------------------------------------------------- */

    static function &getInstance( )
    {
        static $obj;

        if ( !$obj )
        {
            $obj=new ccdsFormFieldHandler();
        }
        return $obj;
    }

    /* ----------------------------------------------------------------------------------------- */

    function register( $className, $fieldype, $add_value_processing=false )
    {
        if ( class_exists( $className ) )
        {
            $this->registeredFields[$fieldype]=$className;
            $this->classes[$className]=new $className();
            $this->add_value_processing[$className]=$add_value_processing;
        }
        else
        {
            trigger_error( "ccdsFormFielHandler->register: given className is not a valid class" );
        }
    }

    /* ----------------------------------------------------------------------------------------- */

    function getFieldTypes( )
    {

        return array_keys( $this->registeredFields );
    }

    /* ----------------------------------------------------------------------------------------- */

    function isRegisteredFieldType( $fieldtype )
    {
        if ( isset( $this->registeredFields[$fieldtype] ) )
            return true;

        return false;
    }
    /* ----------------------------------------------------------------------------------------- */

    function isRegisteredClass( $className )
    {
        if ( in_array( $className, $this->registeredFields ) )
            return true;

        return false;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getFieldOptionArray( $fieltype )
    {

        $className=$this->registeredFields[$fieltype];

        $class=new $className();
        $options=$class->getOptions();
        unset( $class );
        return $options;
    }
    /* ----------------------------------------------------------------------------------------- */

    function getFieldEditArray( $fieldConfig, $fieldname, $value, $project_id=false, $lang_id=false,
        $obj_vers_id=false )
    {
        $fieldType=$fieldConfig['formtype'];

        if ( !$this->isRegisteredFieldType( $fieldType ) )
            return false;

        $className=$this->registeredFields[$fieldType];
        $class=new $className( $project_id, $lang_id );
        $array=$class->getEditArray( $fieldname, $fieldConfig, $value, $obj_vers_id );
        unset( $class );
        return $array;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getFieldPreparedData( $fieldConfig, $fieldname, $values )
    {
        $fieldType=$fieldConfig['formtype'];

        if ( !$this->isRegisteredFieldType( $fieldType ) )
            return false;

        $className=$this->registeredFields[$fieldType];
        $value=$this->classes[$className]->getPreparedData( $fieldConfig, $fieldname, $values );

        return $value;
    }
    /* ----------------------------------------------------------------------------------------- */

    function getFieldPreparedParameterData( $fieldConfig, $fieldname, $values, $obj_id )
    {
        $fieldType=$fieldConfig['formtype'];

        if ( !$this->isRegisteredFieldType( $fieldType ) )
            return false;

        $className=$this->registeredFields[$fieldType];
        $value=$this->classes[$className]->getPreparedParameterData( $fieldConfig, $fieldname, $values, $obj_id );

        return $value;
    }
    /* ----------------------------------------------------------------------------------------- */
//$fieldConfig, $fieldname, $aRow[$fieldname], $obj_vers_id
		function delete( &$fieldConfig, $fieldname, $value, $obj_vers_id=false  ) {

        $className=$this->registeredFields[$fieldConfig['formtype']];
        $this->classes[$className]->delete( $fieldname, $fieldConfig, $value, $obj_vers_id );

		}
		
    function getFieldValue( &$fieldConfig, $fieldname, $value, $obj_vers_id=false )
    {

        $className=$this->registeredFields[$fieldConfig['formtype']];

        if ( empty( $className ) )
        {
            trigger_error( "ccdsFormFieldHandler->getFieldValue: field type '" . $fieldType . "' in fieldname '".$fieldname."' is not registered " );
            return false;
        }

        if ( !$this->add_value_processing )
            return stripslashes( $value );
        $value=$this->classes[$className]->getValue( $fieldname, $fieldConfig, $value, $obj_vers_id );

        return $value;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getFieldParameterValue( &$fieldConfig, $fieldname, $value, $obj_vers_id=false )
    {

        $className=$this->registeredFields[$fieldConfig['formtype']];

        if ( empty( $className ) )
        {
            trigger_error( "ccdsFormFieldHandler->getFieldValue: field type '" . $fieldType . "' is not registered " );

            return false;
        }
        $value=$this->classes[$className]->getParameterValue( $fieldname, $fieldConfig, $value, $obj_vers_id );

        return $value;
    }


    /* ----------------------------------------------------------------------------------------- */

    function getFieldParameterArray( $fieldConfig, $fieldname, $value, $project_id, $language_id )
    {

        $fieldType=$fieldConfig['formtype'];

        if ( !$this->isRegisteredFieldType( $fieldType ) )
        {
            trigger_error( "ccdsFormFieldHandler->getFieldParameterArray: field type '" . $fieldType
                . "' is not registered " );

            return false;
        }
        $className=$this->registeredFields[$fieldType];
        $field=
            $this->classes[$className]->getParameterArray( $fieldConfig, $fieldname, $value, $project_id,
                $language_id );
        return $field;
    }
    /* ----------------------------------------------------------------------------------------- */

    function getFieldConfigArray( $fieldConfig, $project_id=false )
    {

        $fieldType=$fieldConfig['formtype'];

        if ( !$this->isRegisteredFieldType( $fieldType ) )
        {
            trigger_error( "ccdsFormFieldHandler->getFieldValue: field type '" . $fieldType . "' is not registered " );

            return false;
        }
        $className=$this->registeredFields[$fieldType];
        $config=$this->classes[$className]->getFieldConfigArray( $fieldConfig, $project_id );

        foreach ( $config as $key => $value )
        {
            $config[$key]=$this->getFieldEditArray( $value, $key, $fieldConfig[$value['fieldname']] );
        }

        return $config;
    }

/* ----------------------------------------------------------------------------------------- */

}


/* ------------------------------------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------------------------------------ */

class ccdsFieldElement
{
    public $ccdSQL;

    public $tpl_file='does_not_exist.tpl';

    public $options=array();

    public $project_id;
    public $lang_id;

    public $fieldConfig=array
        (
        "label" => array
            (
            'fieldname' => 'label',
            'label' => 'label',
            'formtype' => 'text',
            'value' => '',
            "disabled" => true
            ),
        "length" => array
            (
            'fieldname' => 'length',
            'label' => 'length',
            'formtype' => 'text',
            'value' => '',
            "disabled" => true
            ),
        "type" => array
            (
            'fieldname' => 'type',
            'label' => 'type',
            'formtype' => 'select',
            'value' => array
                (
                array
                    (
                    "label" => "int",
                    "value" => "int"
                    ),
                array
                    (
                    "label" => "bigint",
                    "value" => "bigint"
                    ),
                array
                    (
                    "label" => "text",
                    "value" => "text"
                    ),
                array
                    (
                    "label" => "varchar",
                    "value" => "varchar"
                    ),
                array
                    (
                    "label" => "date",
                    "value" => "date"
                    ),
                array
                    (
                    "label" => "datetime",
                    "value" => "datetime"
                    )
                ),
            "disabled" => true
            )
        );

    /* ----------------------------------------------------------------------------------------- */

    function __construct( $project_id=false, $lang_id=false )
    {
        $this->project_id=$project_id;
        $this->lang_id=$lang_id;
        $this->ccdSQL=ccdSQL::getInstance();
    }

    /* ----------------------------------------------------------------------------------------- */
		
		function delete( $fieldname, &$aConfig, $value, $obj_vers_id=false ) {
			return;
		}

    function getFieldConfigArray( $fieldConfig, $project_id=false )
    {

        $edit_array=array();

        foreach ( $this->fieldConfig as $key => $value )
        {
            $value['selected']=$fieldConfig[$key];
            $edit_array[$key]=$value;
        }

        return $edit_array;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getTplFile( )
    {

        return $this->tpl_file;
    }


    /* ----------------------------------------------------------------------------------------- */

    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        return stripslashes( $value );
    }


    /* ----------------------------------------------------------------------------------------- */

    function getParameterValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        return $value;
    }


    /* ----------------------------------------------------------------------------------------- */

    function getOptions( )
    {
        return $this->options;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $formField=array
            (
            "fieldname" => $fieldname,
            "formtype" => $aConfig['formtype'],
            "selected" => $value,
            "label" => isset( $aConfig['label'] ) ? $aConfig['label'] : false,
            "values" => isset( $aConfig['value'] ) ? $aConfig['value'] : false,
            "userlevel" => isset( $aConfig['userlevel'] ) ? $aConfig['userlevel'] : false,
            "tpl_file" => $this->getTplFile(),
            "disabled" => ( true == $aConfig['disabled'] || 'true' == $aConfig['disabled'] )
                ? $aConfig['disabled'] : false,
            );

        return $formField;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        switch ( $fieldConfig['type'] )
            {
            case "float":
                $ret=str_replace( ",", ".", $values[$fieldname] );
                break;

                default:
                $ret=addslashes($values[$fieldname]);
                break;
            }
        return $ret;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getPreparedParameterData( $fieldConfig, $fieldname, $values, $obj_id=false )
    {
        switch ( $fieldConfig['type'] )
            {
            case "float":
                $ret=str_replace( ",", ".", $values[$fieldname] );
                break;

                default:
                $ret=addslashes($values[$fieldname]);
                break;
            }
        return $ret;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getParameterArray( $fieldConfig, $fieldname, $values, $project_id, $language_id )
    {

        $formField=array
            (
            "fieldname" => $fieldname,
            "formtype" => $fieldConfig['formtype'],
            "selected" => $values,
            "label" => isset( $fieldConfig['label'] ) ? $fieldConfig['label'] : false,
            "values" => isset( $fieldConfig['value'] ) ? $fieldConfig['value'] : false,
            "tpl_file" => $this->getTplFile()
            );

        return $formField;
    }
}


/* ------------------------------------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------------------------------------------------------------ */

class ccdsFieldImage
    extends ccdsFieldElement
{

    public $tpl_file='image.tpl';

    function __construct( $project_id=false, $lang_id=false )
    {
        parent::__construct( $project_id, $lang_id );

        $this->fieldConfig['default_path']=array
            (
            'fieldname' => 'default_path',
            'label' => 'default path',
            'formtype' => 'folderselect',
            'value' => ''
            );
    }

    /* ----------------------------------------------------------------------------------------- */


		function delete( $fieldname, &$aConfig, $value, $obj_vers_id=false ) {

			//print "DELETE IMAGE ".$obj_vers_id."\n";
      if ( false == $obj_vers_id || 0 == $obj_vers_id || '' == $obj_vers_id || $value == '0' ||  $value == '' )
          return;
				
        $query="DELETE FROM pictures WHERE picture_id='" . $value . "'";
        $this->ccdSQL->query($query);
        
	      $query="DELETE FROM pictures WHERE picture_key_fieldname='" . $fieldname . "' AND picture_key_obj_id='"
            . $obj_vers_id . "'";
        $res=$this->ccdSQL->query( $query );            
            			
			return;
		}

    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        if ( false == $obj_vers_id || 0 == $obj_vers_id || '' == $obj_vers_id || $value == '0' ||  $value == '' )
            return;
				
        $ret=array();
        $query="SELECT * FROM pictures WHERE picture_id='" . $value . "'";
        $res=$this->ccdSQL->query( $query );

        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $ret['image']=$row;
        }
        else
        {
	        $query="SELECT * FROM pictures WHERE picture_key_fieldname='" . $fieldname . "' AND picture_key_obj_id='"
            . $obj_vers_id . "'";
	        $res=$this->ccdSQL->query( $query );
	        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
	        {
	            $ret['image']=$row;
	        }
	        else
	        {
  	        $ret['image']=false;
					}
        }
        return $ret;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getParameterValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        return $this->getValue( $fieldname, $aConfig, $value, $obj_vers_id );
    }


    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );
        $query="SELECT * FROM pictures WHERE picture_id='" . $value . "'";
        $res=$this->ccdSQL->query( $query );

        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            
            $config = ccdsConfig::getInstance();
            $tools = ccdsTools::getInstance();
            $iCache = ccdsImageCache::getInstance();
			
            $img_src = $config->get('MEDIA_DIR').'/'.$row['picture_dir'].$row['picture_filename'];
            $size = $iCache->image_info($img_src);
						if (is_array($size))
						$row = array_merge($row,$size);

            $file_size = @filesize($img_src);
						$row['file_size'] = $tools->formatBytes($file_size);
						$row['img_src'] = $img_src;
						$row['end'] = $tools->get_file_extension($img_src);
            $formField['image']=$row;
            
        }
        else
        {
            $formField['image']="";
        }
        $formField['default_path']=isset( $aConfig['default_path'] ) ? $aConfig['default_path'] : '';
        $formField['tpl_file']=$this->getTplFile();
        return $formField;
    }


    /* ----------------------------------------------------------------------------------------- */

    function getParameterArray( $fieldConfig, $fieldname, $value, $project_id, $language_id )
    {
        $formField=parent::getParameterArray( $fieldConfig, $fieldname, $values, $project_id, $language_id );

        $query="SELECT * FROM pictures WHERE picture_id='" . $value . "'";
        $res=$this->ccdSQL->query( $query );

        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $formField['image']=$row;
        }
        else
        {
            $formField['image']="";
        }

        $formField['tpl_file']=$this->getTplFile();

        return $formField;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
    	
    		$values[$fieldname]['dir'] = str_replace('//','/',$values[$fieldname]['dir']);

        $picture_id="";
        $this->ccdSQL->query( "DELETE FROM pictures WHERE picture_key_obj_id='" . $values['obj_vers_id']
            . "' AND picture_key_fieldname='" . $fieldname . "'" );

        if ( $values[$fieldname]['file'] != "" )
        {
            $this->ccdSQL->query( "INSERT INTO pictures
				(
					picture_filename,
					picture_dir,
					picture_description,
					picture_key_fieldname,
					picture_key_obj_id
				)
				VALUES (
					'" . addslashes( $values[$fieldname]['file'] ) . "',
					'" . addslashes( $values[$fieldname]['dir'] ) . "',
					'" . $this->ccdSQL->real_escape_string( $values[$fieldname]['description'] ) . "',
					'" . addslashes( $fieldname ) . "',
					'" . addslashes( $values['obj_vers_id'] ) . "'
				)
			" );
            $picture_id=$this->ccdSQL->insert_id();
            return $picture_id;
        }
        return 0;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getPreparedParameterData( $fieldConfig, $fieldname, $values, $obj_id )
    {
        $picture_id=0;
        $this->ccdSQL->query( "DELETE FROM pictures WHERE picture_key_obj_id='" . $obj_id
            . "' AND picture_key_fieldname='" . $fieldname . "'" );

        if ( $values[$fieldname]['file'] != "" )
        {
            $this->ccdSQL->query( "INSERT INTO pictures
				(
					picture_filename,
					picture_dir,
					picture_description,
					picture_key_fieldname,
					picture_key_obj_id
				)
				VALUES (
					'" . addslashes( $values[$fieldname]['file'] ) . "',
					'" . addslashes( $values[$fieldname]['dir'] ) . "',
					'" . addslashes( $values[$fieldname]['description'] ) . "',
					'" . addslashes( $fieldname ) . "',
					'" . addslashes( $obj_id ) . "'
				)
			" );

            $picture_id=$this->ccdSQL->insert_id();
            return $picture_id;
        }
        return 0;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFieldGallery
    extends ccdsFieldElement
{

    public $tpl_file='gallery.tpl';


    /* ----------------------------------------------------------------------------------------- */
    function __construct( $project_id=false, $lang_id=false )
    {
        parent::__construct( $project_id, $lang_id );

        $this->fieldConfig['default_path']=array
            (
            'fieldname' => 'default_path',
            'label' => 'default path',
            'formtype' => 'folderselect',
            'value' => ''
            );
    }

    /* ----------------------------------------------------------------------------------------- */


		function delete( $fieldname, &$aConfig, $value, $obj_vers_id=false ) {

        $query="DELETE FROM pictures WHERE picture_key_fieldname='" . $fieldname . "' AND picture_key_obj_id='"
            . $obj_vers_id . "'";
        $res=$this->ccdSQL->query( $query );       
            			
			return;
		}

    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        //if ( false == $obj_vers_id )return;

        $ret=array();
        $reg['images']=array();
        $query="SELECT * FROM pictures WHERE picture_key_fieldname='" . $fieldname . "' AND picture_key_obj_id='"
            . $obj_vers_id . "' ORDER BY picture_id";
        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $ret['images'][]=$row;
        }
        return $ret;
    }


    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );
        $query="SELECT * FROM pictures WHERE picture_key_fieldname='" . $fieldname . "' AND picture_key_obj_id='"
            . $obj_vers_id . "' ORDER BY picture_id";
        $res=$this->ccdSQL->query( $query );
        $formField['images']=array();

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $formField['images'][]=$row;
        }
				if (0 == count($formField['images'])) $formField['images']= false;
        $formField['tpl_file']=$this->getTplFile();
        $formField['default_path']=isset( $aConfig['default_path'] ) ? $aConfig['default_path'] : '';

        return $formField;
    }


    /* ----------------------------------------------------------------------------------------- */
    function getPreparedData( $fieldConfig, $fieldname, $values )
    {

        $picture_id=false;
        $this->ccdSQL->query( "DELETE FROM pictures WHERE picture_key_obj_id='" . $values['obj_vers_id']
            . "' AND picture_key_fieldname='" . $fieldname . "'" );

        $i=0;

        if ( is_array( $values[$fieldname]['filename'] ) )
        {
            foreach ( $values[$fieldname]['filename'] as $key => $file )
            {
                $this->ccdSQL->query( "INSERT INTO pictures
								(
									picture_filename,
									picture_dir,
									picture_description,
									picture_key_fieldname,
									picture_key_obj_id
								)
								VALUES (
									'" . addslashes( $values[$fieldname]['filename'][$key] ) . "',
									'" . addslashes( $values[$fieldname]['dir'][$key] ) . "',
									'" . addslashes( $values[$fieldname]['description'][$key] ) . "',
									'" . addslashes( $fieldname ) . "',
									'" . addslashes( $values['obj_vers_id'] ) . "'
								)
							" );
              $i++;
            }
        }
        return $i;
    }
/* ------------------------------------------------------------------------------------------------------------------------------ */

}

// ...................................................................................................................................

class ccdsFieldFile
    extends ccdsFieldElement
{
    public $tpl_file='file.tpl';


    /* ----------------------------------------------------------------------------------------- */
    function __construct( $project_id=false, $lang_id=false )
    {
        parent::__construct( $project_id, $lang_id );

        $this->fieldConfig['default_path']=array
            (
            'fieldname' => 'default_path',
            'label' => 'default path',
            'formtype' => 'folderselect',
            'value' => ''
            );
    }


    /* ----------------------------------------------------------------------------------------- */

		function delete( $fieldname, &$aConfig, $value, $obj_vers_id=false ) {

        $query="DELETE FROM files WHERE file_key_fieldname='" . $fieldname . "' AND file_key_obj_id='". $obj_vers_id . "'";

        $res=$this->ccdSQL->query( $query );       
            			
			return;
		}



    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        if ( false == $obj_vers_id || 0 == $obj_vers_id || '' == $obj_vers_id )
            return;

        $ret=array();
				
				if (!is_numeric($value)) {

            $ret['dir']= preg_replace("@(.*/){0,1}([^/].*?)$@s","$1",$value);
            $ret['name']=preg_replace("@(.*/){0,1}([^/].*?)$@s","$2",$value);

		        $file=split( "\.", $ret['name'] );
		        $file_end=array_pop( $file );

            $ret['end'] =  $file_end;
            return $ret;
				}
        $query="SELECT * FROM files WHERE file_key_fieldname='" . $fieldname . "' AND file_key_obj_id='"
            . $obj_vers_id . "'";
        $res=$this->ccdSQL->query( $query );

        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {

            $ret['dir']=$row['file_dir'];
            $ret['name']=$row['file_name'];
						$ret['descr'] = $row['file_descr'];
		        $file=split( "\.", $ret['name'] );
		        $file_end=array_pop( $file );

            $ret['end'] =  $file_end;
        }
        else
        {
            $ret=false;
        }

        return $ret;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        $picture_id=false;
        $this->ccdSQL->query( "DELETE FROM files WHERE file_key_obj_id='" . $values['obj_vers_id']
            . "' AND file_key_fieldname='" . $fieldname . "'" );


        if ( is_array( $values[$fieldname] ) && !empty($values[$fieldname]['file']) )
        {

	        $this->ccdSQL->query( "INSERT INTO files
						(
							file_name,
							file_dir,
							file_descr,
							file_key_fieldname,
							file_key_obj_id
						)
						VALUES (
							'" . addslashes( $values[$fieldname]['file'] ) . "',
							'" . addslashes( $values[$fieldname]['dir'] ) . "',
							'" . addslashes( $values[$fieldname]['description'] ) . "',
							'" . addslashes( $fieldname ) . "',
							'" . addslashes( $values['obj_vers_id'] ) . "'
						)
					" );
	        return $this->ccdSQL->insert_id();
        } else {
        	return 0;
        }
    }

    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );

        $formField['default_path']=$values['default_path'];

        $query="SELECT * FROM files WHERE file_id='" . $value . "'";
        $res=$this->ccdSQL->query( $query );

        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {

            $formField['file']['dir']=$row['file_dir'];
            $formField['file']['name']=$row['file_name'];

		        $file=split( "\.", $formField['name'] );
		        $file_end=array_pop( $file );
            $formField['file']['end'] =  $file_end;

            $config = ccdsConfig::getInstance();
            $tools = ccdsTools::getInstance();

            $file_src = $config->get('MEDIA_DIR').'/'.$row['file_dir'].$row['file_name'];
            $file_size = @filesize($file_src);
						$formField['file']['file_size'] = $tools->formatBytes($file_size);
						$formField['file']['end'] = $tools->get_file_extension($file_src);
						$formField['file']['end'] = $tools->get_file_extension($file_src);
 						$formField['file']['file_descr'] = $row['file_descr'];

        }
        else
        {
            $formField['file']=false; //array();
        }
        $formField['default_path']=isset( $aConfig['default_path'] ) ? $aConfig['default_path'] : '';
				
        $formField['tpl_file']=$this->getTplFile();

        return $formField;
    }
/* ----------------------------------------------------------------------------------------- */

}

class ccdsFieldFiles
    extends ccdsFieldElement
{

    public $tpl_file='files.tpl';


    /* ----------------------------------------------------------------------------------------- */
    function __construct( $project_id=false, $lang_id=false )
    {
        parent::__construct( $project_id, $lang_id );

        $this->fieldConfig['default_path']=array
            (
            'fieldname' => 'default_path',
            'label' => 'default path',
            'formtype' => 'folderselect',
            'value' => ''
            );
    }

    /* ----------------------------------------------------------------------------------------- */


		function delete( $fieldname, &$aConfig, $value, $obj_vers_id=false ) {

        $query="DELETE FROM files WHERE file_key_fieldname='" . $fieldname . "' AND file_key_obj_id='"
            . $obj_vers_id . "' ORDER BY file_id";
        $res=$this->ccdSQL->query( $query );      
            			
			return;
		}
		
		
    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        if ( false == $obj_vers_id || 0 == $value || '' == $value )
            return;

        $ret=array();
        $reg['images']=array();
        $query="SELECT * FROM files WHERE file_key_fieldname='" . $fieldname . "' AND file_key_obj_id='"
            . $obj_vers_id . "' ORDER BY file_id";
        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $ret['files'][]=$row;
        }
        return $ret;
    }


    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );
        $query="SELECT * FROM files WHERE file_key_fieldname='" . $fieldname . "' AND file_key_obj_id='"
            . $obj_vers_id . "' ORDER BY file_id";
        $res=$this->ccdSQL->query( $query );
        $formField['images']=array();

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $formField['files'][]=$row;
        }

        $formField['tpl_file']=$this->getTplFile();
        $formField['default_path']=empty( $aConfig['default_path'] ) ? '' : $aConfig['default_path'];

        return $formField;
    }


    /* ----------------------------------------------------------------------------------------- */
    function getPreparedData( $fieldConfig, $fieldname, $values )
    {

        $picture_id=false;
        $this->ccdSQL->query( "DELETE FROM files WHERE file_key_obj_id='" . $values['obj_vers_id']
            . "' AND file_key_fieldname='" . $fieldname . "'" );

        $i=0;

        if ( is_array( $values[$fieldname] ) )
        {
            foreach ( $values[$fieldname]['name'] as $key => $file )
            {
              if ( '' != $file )
              {
              	$values[$fieldname]['dir'][$key] = str_replace('//','/',$values[$fieldname]['dir'][$key]);
	              $this->ccdSQL->query( "INSERT INTO files
									(
										file_name,
										file_dir,
										file_descr,
										file_key_fieldname,
										file_key_obj_id
									)
									VALUES (
										'" . addslashes( $values[$fieldname]['name'][$key] ) . "',
										'" . addslashes( $values[$fieldname]['dir'][$key] ) . "',
										'" . addslashes( $values[$fieldname]['descr'][$key] ) . "',
										'" . addslashes( $fieldname ) . "',
										'" . addslashes( $values['obj_vers_id'] ) . "'
									)
								" );
             }
             $i++;
            }
        }
        return $i;
    }
/* ------------------------------------------------------------------------------------------------------------------------------ */

}

// ...................................................................................................................................

class ccdsFieldMultiselect
    extends ccdsFieldElement
{
    public $tpl_file='multiselect.tpl';


    /* ----------------------------------------------------------------------------------------- */
    function __construct( $project_id=false, $lang_id=false )
    {

        parent::__construct( $project_id, $lang_id );
    }

    /* ----------------------------------------------------------------------------------------- */

    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $ret=$value;

        if ( $aConfig['multiple'] == "true" || $aConfig['multiple'] === true )
        {
            $ret=explode( ";", $ret );
            @array_pop( $ret );
            @array_shift( $ret );
        }

        if ( !is_array( $ret ) )
            $res=array($ret);
        return $ret;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getClasses( $aConfig, $project_id,$language_id)
    {
        if ( !$project_id)
            trigger_error( "ccdsFieldmultiselect->getClasses: project_id needed" );

        if ( !$language_id )
            trigger_error( "ccdsFieldmultiselect->getClasses: lang_id needed" );


        $className=$aConfig['class'];
        $values=array();

        $query="
        	SELECT * FROM classes 
        	STRAIGHT_JOIN extensions ON (class_key_ext_id = ext_id)
        	WHERE ext_key_project_id='".$project_id."'
        	AND class_name='" . $className . "'
        	";
        $res=$this->ccdSQL->query( $query );

        if ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $class_id=$row['class_id'];
        }
        else
        {
            trigger_error( "ccdsFieldmultiselect->getClasses: cannot find class " . $className );
            return;
        }
        $query="SELECT obj_id,obj_name_name FROM obj_index 
				STRAIGHT_JOIN classes 
				ON (obj_key_class_id=class_id ) 
				STRAIGHT_JOIN obj_names ON
					(obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='" . $language_id . "')
				LEFT JOIN templates ON
					(obj_key_tpl_id = tpl_id)
				WHERE obj_key_class_id='" . $class_id . "' AND obj_deleted=0 AND obj_link_obj_id =0
		";

        if ( $aConfig['where'] != "" )
        {
            $query.=" AND (" . $aConfig['where'] . ") ";
        }
        $query.="
		ORDER BY obj_name_name";

        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            $values[]=array
                (
                "value" => $row['obj_id'],
                "label" => $row['obj_name_name']
                );
        }

        return $values;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getParameterArray( $fieldConfig, $fieldname, $values, $project_id, $language_id  )
    {

        $formField=parent::getParameterArray( $fieldConfig, $fieldname, $values, $project_id, $language_id );

        if ( $fieldConfig['size'] != "" )
        {
            $formField['size']=$fieldConfig['size'];
        }
        else
        {
            $formField['size']=1;
        }

        $formField['allowEmpty']=( 'false' == $fieldConfig['allowEmpty'] || false == $fieldConfig['allowEmpty'] )
            ? false : true;

        if ( $fieldConfig['multiple'] != "true" )
        {
            $formField['multiple']=false;
        }
        else
        {
            if ( $aConfig['size'] == "" )
                $formField['size']=5;
            $formField['multiple']=true;
            $formField['selected']=explode( ";", $formField['selected'] );
        }

        if ( isset( $fieldConfig['class'] ) && !empty( $fieldConfig['class'] ) )
        {
            $fieldConfig['value']=$this->getClasses( $fieldConfig, $project_id, $language_id );
        }
        elseif ( isset( $fieldConfig['query'] ) && !empty( $aConfig['query'] ) )
        {
            $formField['values']=array();
            $formField['values'][]=array
                (
                "value" => '',
                "label" => '--'
                );

            $res=$this->ccdSQL->query( $fieldConfig['query'] );

            while ( $row=$this->ccdSQL->fetch_array( $res ) )
                $fieldConfig['value'][]=array
                    (
                    "value" => $row[0],
                    "label" => $row[1]
                    );
        }
        elseif ( isset( $fieldConfig['value'] ) )
        {
            if ( !is_array( $fieldConfig['value'] ) )
                $fieldConfig['value']=array($fieldConfig['value']);
        }

        $formField['values']=$fieldConfig['value'];

        if ( !is_array( $formField['selected'] ) )
        {
            $formField['selected']=array($formField['selected']);
        }
        return $formField;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {

        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );

        if ( $aConfig['size'] != "" )
        {
            $formField['size']=$aConfig['size'];
        }
        else
        {
            $formField['size']=1;
        }

        if ( $aConfig['multiple'] != "true" )
        {
            $formField['multiple']=false;
            $formField['allowEmpty']=( 'false' == $aConfig['allowEmpty'] || false == $aConfig['allowEmpty'] )
                ? false : true;
        }
        else
        {
            if ( $aConfig['size'] == "" )
                $formField['size']=5;
            $formField['multiple']=true;
            $formField['selected']=explode( ";", $formField['selected'] );
            $formField['allowEmpty']=false;
        }

        if ( isset( $aConfig['class'] ) && !empty( $aConfig['class'] ) )
        {
            $formField['values']=$this->getClasses( $aConfig, $this->project_id, $this->lang_id );
        }
        elseif ( isset( $aConfig['query'] ) && !empty( $aConfig['query'] ) )
        {
            $formField['values']=array();
            $res=$this->ccdSQL->query( $aConfig['query'] );
            while ( $row=$this->ccdSQL->fetch_array( $res ) )
                $formField['values'][]=array
                    (
                    "value" => $row[0],
                    "label" => $row[1]
                    );
        }

        if ( !is_array( $formField['selected'] ) )
            $formField['selected']=array($formField['selected']);

        return $formField;
    }
    /* ----------------------------------------------------------------------------------------- */
    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        if ( is_array( $values[$fieldname] ) )
        {
        	if (0 == count($values[$fieldname]))
        		$ret = ';';
        	else
            $ret=";" . implode( ";", $values[$fieldname] ) . ";";
        }
        else
        {
            $ret=$values[$fieldname];
        }
        return $ret;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getFieldConfigArray( $fieldConfig, $project_id=false )
    {

        $this->fieldConfig['multiple']=array
            (
            'fieldname' => 'multiple',
            'label' => 'multiple',
            'formtype' => 'select',
            'allowEmpty' => false,
            'value' => array
                (
                array
                    (
                    "value" => 'false',
                    "label" => 'no'
                    ),
                array
                    (
                    "value" => 'true',
                    "label" => 'yes'
                    )
                )
            );

        $this->fieldConfig['allowEmpty']=array
            (
            'fieldname' => 'allowEmpty',
            'label' => 'allowEmpty values',
            'formtype' => 'select',
            'allowEmpty' => false,
            'value' => array
                (
                array
                    (
                    "value" => 'false',
                    "label" => 'no'
                    ),
                array
                    (
                    "value" => 'true',
                    "label" => 'yes'
                    )
                )
            );

        $this->fieldConfig['size']=array
            (
            'fieldname' => 'size',
            'label' => 'size',
            'formtype' => 'text',
            'allowEmpty' => false,
            'value' => ''
            );

        $this->fieldConfig['query']=array
            (
            'fieldname' => 'query',
            'label' => 'sql query',
            'formtype' => 'text',
            'allowEmpty' => true,
            'value' => ''
            );

        $this->fieldConfig['class']=array
            (
            'fieldname' => 'class',
            'label' => 'class',
            'formtype' => 'multiselect',
            'allowEmpty' => false,
            'query'
                =>
                    'SELECT class_name,class_name FROM classes STRAIGHT JOIN extensions ON class_key_ext_id = ext_id WHERE ext_key_project_id='
            . $project_id,
            'value' => ''
            );

        $this->fieldConfig['where']=array
            (
            'fieldname' => 'where',
            'label' => 'WHERE-CLAUSE (optional)',
            'formtype' => 'text',
            'allowEmpty' => false,
            'value' => ''
            );

        $edit_array=array();

        foreach ( $this->fieldConfig as $key => $value )
        {
            $value['selected']=$fieldConfig[$key];
            $edit_array[$key]=$value;
        }
        return $edit_array;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

}

class ccdsFieldStructureSelect
    extends ccdsFieldElement
{
    public $tpl_file='structureselect.tpl';


    /* ----------------------------------------------------------------------------------------- */
    function __construct( $project_id=false, $lang_id=false )
    {

        parent::__construct( $project_id, $lang_id );
        $this->tools = ccdsTools::getInstance();

    }

    /* ----------------------------------------------------------------------------------------- */

    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $ret=$value;

        if ( $aConfig['multiple'] == "true" || $aConfig['multiple'] === true )
        {
            $ret=explode( ";", $ret );
            @array_pop( $ret );
            @array_shift( $ret );
        }

        if ( !is_array( $ret ) )
            $res=array($ret);
        return $ret;
    }


		function getNaviStructure($root_id,$project_id=false,$class=false) {
			$results = array();				

			if ($class !== false) {
				$classes = explode(",",$class);
			}
			if ($project_id != false) {$project_where = " AND obj_key_project_id='".$project_id."'";} else {$project_where="";}
			$query = "SELECT * FROM obj_index LEFT JOIN classes ON obj_key_class_id=class_id LEFT OUTER JOIN obj_names ON (obj_name_key_obj_id = obj_id AND obj_name_key_lang_id='".$this->lang_id."') WHERE obj_parent_obj_id = '".$root_id."' AND obj_deleted=0 ".$project_where." AND obj_no_structure=0  ORDER BY obj_position";
			$res = $this->ccdSQL->query($query);
			while ($row = $this->ccdSQL->fetch_array($res)) {
					if ($row['obj_name_name'] == "") $row['obj_name_name'] = $this->tools->getAlternativeObjName($row['obj_id']);
					if ($class != false) {
						if (in_array($row['class_name'],$classes)) {
							$results[]=array("name" => htmlentities($row['obj_name_name'],ENT_QUOTES,"UTF-8"),"value" => $row['obj_id'],"subpages" => $this->getNaviStructure($row['obj_id'],$project_id,$class));
						}
					} else {
						$results[]=array("name" => htmlentities($row['obj_name_name'],ENT_QUOTES,"UTF-8"),"value" => $row['obj_id'],"subpages" => $this->getNaviStructure($row['obj_id'],$project_id,$class));
					}
			}		
			return $results;
		}

    /* ----------------------------------------------------------------------------------------- */

    function getParameterArray( $fieldConfig, $fieldname, $values, $project_id, $language_id  )
    {

        $formField=parent::getParameterArray( $fieldConfig, $fieldname, $values, $project_id, $language_id );

        if ( $fieldConfig['size'] != "" )
        {
            $formField['size']=$fieldConfig['size'];
        }
        else
        {
            $formField['size']=1;
        }

        $formField['allowEmpty']=( 'false' == $fieldConfig['allowEmpty'] || false == $fieldConfig['allowEmpty'] )
            ? false : true;

        if ( $fieldConfig['multiple'] != "true" )
        {
            $formField['multiple']=false;
        }
        else
        {
            if ( $aConfig['size'] == "" )
                $formField['size']=5;
            $formField['multiple']=true;
            $formField['selected']=explode( ";", $formField['selected'] );
        }

        if ( isset( $fieldConfig['class'] ) && !empty( $fieldConfig['class'] ) )
        {
            $fieldConfig['value']=$this->getClasses( $fieldConfig, $project_id, $language_id );
        }
        elseif ( isset( $fieldConfig['query'] ) && !empty( $aConfig['query'] ) )
        {
            $formField['values']=array();
            $formField['values'][]=array
                (
                "value" => '',
                "label" => '--'
                );

            $res=$this->ccdSQL->query( $fieldConfig['query'] );

            while ( $row=$this->ccdSQL->fetch_array( $res ) )
                $fieldConfig['value'][]=array
                    (
                    "value" => $row[0],
                    "label" => $row[1]
                    );
        }
        elseif ( isset( $fieldConfig['value'] ) )
        {
            if ( !is_array( $fieldConfig['value'] ) )
                $fieldConfig['value']=array($fieldConfig['value']);
        }

        $formField['values']=$fieldConfig['value'];

        if ( !is_array( $formField['selected'] ) )
        {
            $formField['selected']=array($formField['selected']);
        }
        return $formField;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {

        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );
 
        if ( $aConfig['size'] != "" )
        {
            $formField['size']=$aConfig['size'];
        }
        else
        {
            $formField['size']=1;
        }

        if ( $aConfig['multiple'] != "true" )
        {
            $formField['multiple']=false;
            $formField['allowEmpty']=( 'false' == $aConfig['allowEmpty'] || false == $aConfig['allowEmpty'] )
                ? false : true;
        }
        else
        {
            if ( $aConfig['size'] == "" )
                $formField['size']=5;
            $formField['multiple']=true;
            $formField['selected']=explode( ";", $formField['selected'] );
            $formField['allowEmpty']=false;
        }
		
				$root = isset($aConfig['root']) ? $aConfig['root'] : 0;
				$classes = isset($aConfig['class']) ? $aConfig['class'] : false;


				$formField['values'] = $this->getNaviStructure($root, $this->project_id, $classes);

        if ( !is_array( $formField['selected'] ) )
            $formField['selected']=array($formField['selected']);

        return $formField;
    }
    /* ----------------------------------------------------------------------------------------- */
    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        if ( is_array( $values[$fieldname] ) )
        {
            $ret=";" . implode( ";", $values[$fieldname] ) . ";";
        }
        else
        {
            $ret=$values[$fieldname];
        }
        return $ret;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getFieldConfigArray( $fieldConfig, $project_id=false )
    {

        $this->fieldConfig['multiple']=array
            (
            'fieldname' => 'multiple',
            'label' => 'multiple',
            'formtype' => 'select',
            'allowEmpty' => false,
            'value' => array
                (
                array
                    (
                    "value" => 'false',
                    "label" => 'no'
                    ),
                array
                    (
                    "value" => 'true',
                    "label" => 'yes'
                    )
                )
            );

        $this->fieldConfig['allowEmpty']=array
            (
            'fieldname' => 'allowEmpty',
            'label' => 'allowEmpty values',
            'formtype' => 'select',
            'allowEmpty' => false,
            'value' => array
                (
                array
                    (
                    "value" => 'false',
                    "label" => 'no'
                    ),
                array
                    (
                    "value" => 'true',
                    "label" => 'yes'
                    )
                )
            );

        $this->fieldConfig['size']=array
            (
            'fieldname' => 'size',
            'label' => 'size',
            'formtype' => 'text',
            'allowEmpty' => false,
            'value' => ''
            );

        $this->fieldConfig['query']=array
            (
            'fieldname' => 'query',
            'label' => 'sql query',
            'formtype' => 'text',
            'allowEmpty' => true,
            'value' => ''
            );

        $this->fieldConfig['class']=array
            (
            'fieldname' => 'class',
            'label' => 'class',
            'formtype' => 'multiselect',
            'allowEmpty' => false,
            'query'
                =>
                    'SELECT class_name,class_name FROM classes STRAIGHT JOIN extensions ON class_key_ext_id = ext_id WHERE ext_key_project_id='
            . $project_id,
            'value' => ''
            );

        $this->fieldConfig['where']=array
            (
            'fieldname' => 'where',
            'label' => 'WHERE-CLAUSE (optional)',
            'formtype' => 'text',
            'allowEmpty' => false,
            'value' => ''
            );

        $edit_array=array();

        foreach ( $this->fieldConfig as $key => $value )
        {
            $value['selected']=$fieldConfig[$key];
            $edit_array[$key]=$value;
        }
        return $edit_array;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

}

/* ------------------------------------------------------------------------------------------------------------------------------ */

class ccdsFieldInternallink
    extends ccdsFieldElement
{
    /* ----------------------------------------------------------------------------------------- */
    public $tpl_file='multiselect.tpl';


    /* ----------------------------------------------------------------------------------------- */
    function __construct( $project_id=false, $lang_id=false )
    {
        parent::__construct( $project_id, $lang_id );

        $this->fieldConfig['multiple']=array
            (
            'fieldname' => 'multiple',
            'label' => 'multiple',
            'formtype' => 'select',
            'allowEmpty' => false,
            'value' => array
                (
                array
                    (
                    "value" => 'false',
                    "label" => 'no'
                    ),
                array
                    (
                    "value" => 'true',
                    "label" => 'yes'
                    )
                )
            );

        $this->fieldConfig['allowEmpty']=array
            (
            'fieldname' => 'allowEmpty',
            'label' => 'allowEmpty values',
            'formtype' => 'select',
            'allowEmpty' => false,
            'value' => array
                (
                array
                    (
                    "value" => 'false',
                    "label" => 'no'
                    ),
                array
                    (
                    "value" => 'true',
                    "label" => 'yes'
                    )
                )
            );

        $this->fieldConfig['size']=array
            (
            'fieldname' => 'size',
            'label' => 'size',
            'formtype' => 'text',
            'allowEmpty' => false,
            'value' => ''
            );

        $this->fieldConfig['class']=array
            (
            'fieldname' => 'class',
            'label' => 'class (optional)',
            'formtype' => 'text',
            'allowEmpty' => false,
            'value' => ''
            );

        $this->fieldConfig['root']=array
            (
            'fieldname' => 'root',
            'label' => 'root object (optional)',
            'formtype' => 'text',
            'allowEmpty' => false,
            'value' => ''
            );

        $this->fieldConfig['where']=array
            (
            'fieldname' => 'where',
            'label' => 'WHERE-CLAUSE (optional)',
            'formtype' => 'text',
            'allowEmpty' => false,
            'value' => ''
            );
    }

    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        if ( false == $obj_vers_id )
            return;

        $ret=$value;

        if ( $aConfig['multiple'] == "true" )
        {
            $ret=explode( ";", $ret );
            @array_pop( $ret );
            @array_shift( $ret );
        }
        return $ret;
    }

    function getParameterValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $ret=$value;

        if ( $aConfig['multiple'] == "true" )
        {
            $ret=explode( ";", $ret );
            @array_pop( $ret );
            @array_shift( $ret );
        }
        return $ret;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getNaviSerialized( $root_id, $lastpages, $project_id=false, $language_id, $class=false, $where=false )
    {

        $results=array();

        if ( $class != false )
        {
            $classes=explode( ",", $class );
        }

        if ( $project_id != false )
        {
            $project_where=" AND obj_key_project_id='" . $project_id . "'";
        }
        else
        {
            $project_where="";
        }
        $query="SELECT * FROM obj_index 
			LEFT JOIN classes 
				ON obj_key_class_id=class_id 
			LEFT OUTER JOIN obj_names 
				ON (obj_name_key_obj_id = obj_id 
				AND obj_name_key_lang_id='" . $language_id . "') 
			WHERE obj_parent_obj_id = '" . $root_id . "' AND obj_deleted=0 " . $project_where . " AND obj_no_structure=0 
			";

        if ( false != $where )
        {
            $query.=" AND (" .$where.") ";
				}
        $query.=" ORDER BY obj_position";

        //print_r($query);

        $res=$this->ccdSQL->query( $query );

        while ( $row=$this->ccdSQL->fetch_assoc( $res ) )
        {
            if ( "" == $row['obj_name_name'] )
            {
                $foo=new ccdsObject( $row['obj_id'] );
                $name=$foo->getName( $this->lang_id );
                $row['obj_name_name']=$name['obj_name_name'];
            }

            if ( $class != false )
            {
                if ( in_array( $row['class_name'], $classes ) )
                {
                    $results[]=array
                        (
                        "label" => htmlentities( $lastpages . $row['obj_name_name'], ENT_QUOTES, "UTF-8" ),
                        "value" => $row['obj_id'],
                        "url" => "/" . $row['obj_id'] . "/" . $row['obj_name_serialized']
                        );
                }
            }
            else
            {
                $results[]=array
                    (
                    "label" => htmlentities( $lastpages . $row['obj_name_name'], ENT_QUOTES, "UTF-8" ),
                    "value" => $row['obj_id'],
                    "url" => "/" . $row['obj_id'] . "/" . $row['obj_name_serialized']
                    );
            }

            $tmp_array=
                $this->getNaviSerialized( $row['obj_id'], $lastpages . $row['obj_name_name'] . ">", $project_id,
                    $language_id, $class, $where );

            $results=array_merge( $results, $tmp_array );
        }

        return $results;
    }

    /* ----------------------------------------------------------------------------------------- */
    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        if ( 'true' == $fieldConfig['multiple'] )
        {
            $ret=";" . implode( ";", $values[$fieldname] ) . ";";
        }
        else
        {
            $ret=$values[$fieldname];
        }
        return $ret;
    }
    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );

        if ( $aConfig['root'] != "" )
        {
            $root=$aConfig['root'];
        }
        else
        {
            $root=0;
        }

        if ( ( $aConfig['class'] != "" ) AND ( $aConfig['class'] != "*" ) )
        {
            $class=$aConfig['class'];
        }
        else
        {
            $class=false;
        }

        if ( $aConfig['size'] != "" )
        {
            $formField['size']=$aConfig['size'];
        }
        else
        {
            $formField['size']=1;
        }

        if ( $aConfig['multiple'] != "true" )
        {
            $formField['multiple']=false;
            $formField['allowEmpty']=( 'false' == $aConfig['allowEmpty'] || false == $aConfig['allowEmpty'] )
                ? false : true;
        }
        else
        {
            $formField['multiple']=true;
            $formField['selected']=explode( ";", $formField['selected'] );
        }

        if ( !is_array( $formField['selected'] ) )
            $formField['selected']=array($formField['selected']);

        if ( ( $aConfig['where'] != "" ) AND ( $aConfig['where'] != "*" ) )
        {
            $where=$aConfig['where'];
        }
        else
        {
            $where=false;
        }

        $formField['values']=$this->getNaviSerialized( $root, "", $project_id, $language_id, $class, $where );
        return $formField;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getParameterArray( $fieldConfig, $fieldname, $values, $project_id, $language_id )
    {

        $formField=parent::getParameterArray( $fieldConfig, $fieldname, $values, $project_id, $language_id );

        if ( is_int( $fieldConfig['root'] ) )
        {
            $root=$fieldConfig['root'];
        }
        else
        {
            $root=0;
        }

        if ( ( $fieldConfig['class'] != "" ) AND ( $fieldConfig['class'] != "*" ) )
        {
            $class=$fieldConfig['class'];
        }
        else
        {
            $class=false;
        }

        if ( $fieldConfig['size'] != "" )
        {
            $formField['size']=$fieldConfig['size'];
        }
        else
        {
            $formField['size']=1;
        }

        if ( $fieldConfig['multiple'] != "true" )
        {
            $formField['multiple']=false;
            $formField['allowEmpty']=( 'false' == $fieldConfig['allowEmpty'] || false == $fieldConfig['allowEmpty'] )
                ? false : true;
        }
        else
        {
            $formField['multiple']=true;
            $formField['selected']=explode( ";", $formField['selected'] );
        }

        if ( !is_array( $formField['selected'] ) )
            $formField['selected']=array($formField['selected']);

        if ( ( $fieldConfig['where'] != "" ) AND ( $fieldConfig['where'] != "*" ) )
        {
            $where=$fieldConfig['where'];
        }
        else
        {
            $where=false;
        }
        $rows=$this->getNaviSerialized( $root, "", $project_id, $language_id, $class, $where );

        $formField['values']=$rows;
        //$formField['values'] = $this->getNaviSerialized( $root, "", false,$project_id,$language_id, $class, $where );

        return $formField;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

}


//// ...................................................................................................................................

class ccdsFieldHidden
    extends ccdsFieldElement
{
    public $tpl_file='hidden.tpl';

/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFolderSelect
    extends ccdsFieldElement
{
    public $tpl_file='folderselect.tpl';

/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFieldhtml
    extends ccdsFieldElement
{


	
		function parser($matches) {
			
			$config = ccdsConfig::getInstance();
			
			$tag = $matches[0];
			
			if (0 == strpos('http',$matches[1])) return $tag;
			$src = $config->get( 'DOC_ROOT' ) . $config->get( 'WEB_ROOT' ).$matches[1];

			$regex = '/width="([0-9]*)"/';
			$res = array();
			preg_match($regex,$tag,$res);
			$width = $res[1];
		
			$regex = '/height="([0-9]*)"/';
			$res = array();
			preg_match($regex,$tag,$res);
			$height = $res[1];
			if ( (0 < $width || 0 < $height)  ) {
				$iCache = ccdsImageCache::getInstance();
				$config = ccdsConfig::getInstance();
				
				$img_src =  $iCache->img_cache_resize($src,$width,$height);
	      $return=
	          "<img id=\"" . $img_id . "\" border=\"" . $border . "\" " . $align . " title=\"" . $title . "\" src=\""
	          . $img_src['src'] . "\"  alt=\"" . $img_alt . "\" width=\"" . $img_src['width'] . "\" height=\""
	          . $img_src['height'] . "\" " . $style . " />";			
				return $return;
			} else {
				return $tag;
			}		
		}
			

    function getValue( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
        $value = stripslashes($value);
				$regex = '/<img.*?src="([^"].*?)".*?\/\>/';
				$value = preg_replace_callback($regex,array($this,'parser'),$value);
        return stripslashes( $value );
    }
	
	
	
    public $tpl_file='html.tpl';
/* ------------------------------------------------------------------------------------------------------------------------------ */


/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFieldtext
    extends ccdsFieldElement
{
    public $tpl_file='text.tpl';


    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {
			
			$formField = parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id);
			
			$formField['password'] = ('true' == $aConfig['password'] || true == $aConfig['password']) ? true : false;
						
			return $formField;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */


/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFieldGeshi
    extends ccdsFieldElement
{
    public $tpl_file='geshi.tpl';
/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getValue( &$fieldConfig, $fieldname, $value, $obj_vers_id=false )
    {
        $aValue=unserialize( $value );

        $config=ccdsConfig::getInstance();
        $path=$config->get( LIBRARY_DIR );
        $path.='geshi/geshi/';
        $geshi=new GeSHi( $aValue['value'], $aValue['lang'], $path );

        return $geshi->parse_code();
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getFieldValue( &$fieldConfig, $fieldname, $value, $obj_vers_id=false )
    {
        $aValue=unserialize( $value );

        return $aValue[$fieldname]['value'];
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        $aValues=array
            (
            'value' => $values[$fieldname]['value'],
            'lang' => $values[$fieldname]['lang']
            );

        return serialize( $aValues );
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getEditArray( $fieldname, $aConfig, $value, $obj_vers_id=false )
    {
        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );
        $formField['selected']=unserialize( $formField['selected'] );

        return $formField;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getParameterValue( &$fieldConfig, $fieldname, $value, $obj_vers_id=false )
    {
        $config=ccdsConfig::getInstance();
        $path=$config->get( LIBRARY_DIR );
        $path.='geshi/geshi/';
        $geshi=new GeSHi( $value['value'], $value['lang'], $path );

        return $geshi->parse_code();
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getPreparedParameterData( $fieldConfig, $fieldname, $values )
    {
        $aValues=array
            (
            'value' => $values[$fieldname]['value'],
            'lang' => $values[$fieldname]['lang']
            );

        return $aValues;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFieldplaintext
    extends ccdsFieldElement
{
    public $tpl_file='plaintext.tpl';
/* ------------------------------------------------------------------------------------------------------------------------------ */


/* ------------------------------------------------------------------------------------------------------------------------------ */

}

// ...................................................................................................................................

class ccdsFielddate
    extends ccdsFieldElement
{
    public $tpl_file='date.tpl';
    /* ----------------------------------------------------------------------------------------- */

    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        return $values[$fieldname]["year"] . "-" . $values[$fieldname]["month"] . "-" . $values[$fieldname]["day"];
    }

    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, $aConfig, $value, $obj_vers_id=false )
    {

        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );

        if ( ( $formField['selected'] == "0000-00-00" ) OR ( $formField['selected'] == "" ) )
        {
            $formField['selected_day']=date( "d" );
            $formField['selected_month']=date( "m" );
            $formField['selected_year']=date( "Y" );
        }
        else
        {
            $date=strtotime( $formField['selected'] );
            $formField['selected_day']=date( "d", $date );
            $formField['selected_month']=date( "m", $date );
            $formField['selected_year']=date( "Y", $date );
        }

        $formField['days']=array();

        for ( $i=1; $i <= 31; $i++ )
            $formField['days'][]=$i;
        $formField['months']=array();

        for ( $i=1; $i <= 12; $i++ )
            $formField['months'][]=$i;

        return $formField;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFielddatetime
    extends ccdsFieldElement
{
    public $tpl_file='datetime.tpl';
    /* ----------------------------------------------------------------------------------------- */
    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        return $values[$fieldname]['year'] . '-' . $values[$fieldname]['month'] . '-' . $values[$fieldname]['day'] . ' '
            . $values[$fieldname]['hour'] . ":" . $values[$fieldname]['minute'] . ':00';
    }

    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, $aConfig, $value, $obj_vers_id=false )
    {

        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );

        if ( ( $formField['selected'] == '0000-00-00 00:00:00' ) OR ( $formField['selected'] == '' ) )
        {
            $formField['selected_hour']=date( 'H' );
            $formField['selected_minute']=date( 'i' );

            $formField['selected_day']=date( 'd' );
            $formField['selected_month']=date( 'm' );
            $formField['selected_year']=date( 'Y' );
        }
        else
        {
            $date=strtotime( $formField['selected'] );
            $formField['selected_hour']=date( 'H', $date );
            $formField['selected_minute']=date( 'i', $date );

            $formField['selected_day']=date( 'd', $date );
            $formField['selected_month']=date( 'm', $date );
            $formField['selected_year']=date( 'Y', $date );
        }

        $formField['days']=array();

        for ( $i=1; $i <= 31; $i++ )
            $formField['days'][]=$i;
        $formField['months']=array();

        for ( $i=1; $i <= 12; $i++ )
            $formField['months'][]=$i;

        return $formField;
    }
/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFieldfieldset
    extends ccdsFieldElement
{
    public $tpl_file='fieldset.tpl';
/* ------------------------------------------------------------------------------------------------------------------------------ */


/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

class ccdsFielddynamic
    extends ccdsFieldElement
{
    public $tpl_file='dynamic.tpl';
    /* ----------------------------------------------------------------------------------------- */

    function getValue( $fieldname, $aConfig, $value, $obj_vers_id=false )
    {
        $values=unserialize( stripslashes( $value ) );
        $formField=array();

        if ( is_array( $values ) )
            foreach ( $values as $key => $value )
            {

                $class="ccdsField" . $value['formtype'];

                if ( class_exists( $class ) )
                {
                    $field=new $class( $fieldname . "[" . $key . "][value]", $values );
                    $ret=$field->getValue( $value['value'] );
                }

                $formField[]=array
                    (
                    "label" => $key,
                    "fieldname" => $fieldname . "[" . $key . "][value]",
                    "selected" => $ret,
                    "type" => $value['type']
                    );
            }

        if ( 0 == count( $formField ) )
            return false;

        return $formField;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getEditArray( $fieldname, $aConfig, $value, $obj_vers_id=false )
    {

        $formField=parent::getEditArray( $fieldname, $aConfig, $project_id, false );

        $formField['fields']=array();

        $values=unserialize( stripslashes( $value ) );

        if ( is_array( $values ) )
            foreach ( $values as $key => $value )
            {

                $class="ccdsField" . $value['formtype'];

                if ( class_exists( $class ) )
                {
                    $field=new $class( $key, $values );
                    $ret=$field->getValue( $value['value'] );
                }

                $formField['fields'][]=array
                    (
                    "label" => $key,
                    "fieldname" => $key,
                    "suffix" => "[value]",
                    "selected" => $ret,
                    "formtype" => $value['formtype']
                    );
            }

        if ( 0 == count( $formField['fields'] ) )
            $formField['fields']=false;

        return $formField;
    }

    /* ----------------------------------------------------------------------------------------- */

    function getPreparedData( $fieldConfig, $fieldname, $values )
    {

        $field=$values[$fieldname];

        if ( is_array( $field['fields'] ) )
            foreach ( $field['fields'] as $key => $value )
            {
                $class="ccdsField" . $value['formtype'];

                if ( class_exists( $class ) )
                {
                    $field=new $class( $key, $values );
                    $field['fields'][$key]['value']=$field->getPreparedData( $value['value'] );
                }
            }
        return addslashes( serialize( $values[$fieldname] ) );
    }
/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

// ...................................................................................................................................

class ccdsFieldPrice
    extends ccdsFieldElement
{
    public $tpl_file='price.tpl';
/* ------------------------------------------------------------------------------------------------------------------------------ */

    function getPreparedData( $fieldConfig, $fieldname, $values )
    {
        $value=$values[$fieldname]['netto'];
        $value=str_replace( ",", ".", $value );
        return $value;
    }

    function getEditArray( $fieldname, &$aConfig, $value, $obj_vers_id=false )
    {

        $formField=parent::getEditArray( $fieldname, $aConfig, $value, $obj_vers_id );
        $formField['taxfield']=$aConfig['taxfield'];

        if ( '' == $formField['selected'] )
            $formField['selected']=0;
        return $formField;
    }

/* ------------------------------------------------------------------------------------------------------------------------------ */

}
// ...................................................................................................................................

$formHandler=ccdsFormFieldHandler::getInstance();

$formHandler->register( "ccdsFieldtext", "text" );
$formHandler->register( "ccdsFieldGeshi", "geshi" );
$formHandler->register( "ccdsFieldplaintext", "plaintext" );
$formHandler->register( "ccdsFieldhtml", "html" );

$formHandler->register( "ccdsFieldImage", "image", true );
$formHandler->register( "ccdsFieldGallery", "gallery", true );

$formHandler->register( "ccdsFieldFile", "file", true );
$formHandler->register( "ccdsFieldFiles", "files", true );

$formHandler->register( "ccdsFielddate", "date" );
$formHandler->register( "ccdsFielddatetime", "datetime" );

$formHandler->register( "ccdsFieldMultiselect", "select", true );

$formHandler->register( "ccdsFieldMultiselect", "multiselect", true );
$formHandler->register( "ccdsFieldMultiselect", "multiselectclass", true );
$formHandler->register( "ccdsFieldInternallink", "internallink" );

$formHandler->register( "ccdsFieldfieldset", "fieldset", true );

$formHandler->register( "ccdsFielddynamic", "dynamic", true );
$formHandler->register( "ccdsFieldHidden", "hidden" );
$formHandler->register( "ccdsFolderSelect", "folderselect" );

$formHandler->register( "ccdsFieldPrice", "price" );

$formHandler->register( "ccdsFieldStructureSelect", "structureselect" );

